package com.gimi.cloud.bbp.dao.dao.mall;

import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.gimi.cloud.bbp.dao.dao.sys.BaseDao;
import com.gimi.cloud.bbp.dao.myMapper.MyMapper;
import com.gimi.cloud.bbp.dto.mall.MallAppSpecialGroupDTO;
import com.gimi.cloud.bbp.entity.mall.MallAppSpecialGroupEntity;

/**
 * 专场组数据访问层
 * @author chendi
 * @date 2018年1月31日
 */
public interface MallAppSpecialGroupDao extends MyMapper<MallAppSpecialGroupEntity>,BaseDao<MallAppSpecialGroupEntity,MallAppSpecialGroupDTO>{

	@Select("SELECT * FROM `mall_app_special_group` WHERE asg_predict_start_time <= NOW() AND asg_predict_end_time >= NOW() AND asg_status = 1 AND  is_delete = 0")
	@Results({
		@Result(column="asg_id", property="specials",  many =@Many(select ="com.gimi.cloud.bbp.dao.dao.mall.MallAppSpecialDao.getDTOByAsgIdLiMit3")),
		@Result(column="asg_id", property="asgId")
	})
	List<MallAppSpecialGroupDTO> selectNowStart();
	
	@Select("SELECT * FROM `mall_app_special_group` WHERE is_delete = 0 AND asg_id = #{asgId} LIMIT 1")
	MallAppSpecialGroupDTO selectDTOByAsgId(Integer asgId);
	
	@Update("UPDATE  mall_app_special_group SET asg_start_time = NOW() WHERE asg_predict_start_time <= NOW() AND asg_predict_end_time >= NOW() AND asg_start_time IS NULL  AND asg_status = 1 AND  is_delete = 0 ")
	Long updateStartTimeByNow();
	
	@Update("UPDATE mall_app_special_group SET asg_end_time = NOW() WHERE asg_predict_end_time <= NOW() AND asg_end_time IS NULL AND asg_status = 1 AND  is_delete = 0")
	Long updateEndTimeByNow();

	
}
